﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using ChinapnrAPI;
using DTCash.Logger.DTO;
using DTCash.Logger.NodeLog.Model;
using DTCash.Redis;
using Uranus.Service.OrderService;
using Uranus.Service.OtherService;
using Uranus.Service.UserService;
using Uranus.WeiStationAPI.App_Start;
using Uranus.WeiStationAPI.Common;
using Uranus.WeiStationAPI.Models;
using DTCash.Logger;
using Uranus.Common;
using BaseResponse = Uranus.Service.UserService.BaseResponse;

namespace Uranus.WeiStationAPI.Controllers
{
    public class ChinaPnrController : BaseController
    {
        /// <summary>
        /// 获取huifu地址
        /// </summary>
        /// <param name="gid">标志</param>
        /// <param name="type">类型</param>
        /// <returns></returns>
        [HttpPost]
        [UserState(VisitUserStateEnum = VisitUserStateEnum.MustLogin)]
        public ResultResPonse GetHuiFuUrl()
        {
            if (UserInfo.UserID != 0)
            {
                using (
                    DTCash.DistributedLock.IDistributedLock iDistributedLock =
                        DTCash.DistributedLock.DistributedLock.GetLockByNoTimeOutDisposable($"gethuifuurl_{UserInfo.UserID}",
                            new TimeSpan(0, 1, 0)))
                {
                    if (!iDistributedLock.LockDto.Success)
                    {
                        resultResPonse.code = 18;
                        return Send(resultResPonse);
                    }
                    string gid = GetParmsKey<string>("gid");
                    string type = GetParmsKey<string>("type");
                    string pagetype = GetParmsKey<string>("pagetype"); //页面样式
                    if (string.IsNullOrEmpty(pagetype))
                    {
                        pagetype = "1";
                    }
                    if (string.IsNullOrEmpty(gid) || string.IsNullOrEmpty(type))
                    {
                        resultResPonse.code = 3; //参数非法 
                        return Send(resultResPonse);
                    }
                    UserShopInfo userShopInfo =
                        CacheRedis.GetCache(CacheItem.InitByIdentity(gid, "Confirmation").Other.other) as UserShopInfo;
                    if (userShopInfo != null)
                    {
                        if (type.ToLower() == "register")
                        {
                            #region 注册 汇付的

                            try
                            {
                                TransactionResultCommon transactionResultCommon = new TransactionResultCommon()
                                {
                                    CmdID = CmdID.UserRegister,
                                    UserID = UserInfo.UserID
                                };
                                string guid = Guid.NewGuid().ToString("N");
                                CacheRedis.AddCache(new TimeSpan(5, 0, 0),
                                    CacheItem.InitByIdentity("HuiFuTransaction", guid).Other.other,
                                    transactionResultCommon);

                                ChinapnrUserManagement chinapnrUserManagement = new ChinapnrUserManagement();
                                chinapnrUserManagement.PageType = pagetype;

                                string url =
                                    chinapnrUserManagement.UserRegister(
                                        "ajc" + UserInfo.UserID.ToString().PadLeft(10, '0'), "", "", UserInfo.Mobile,
                                        UserInfo.Email ?? "",
                                        UserInfo.UserID + "",
                                        ChinapnrCommon.ChinaReceiveUrl,
                                        ChinapnrCommon.ChinaResponseUrl);

                                //LogHelper.WriteLogInfo("开户", url, new { UserInfo.UserID });
                                Dictionary<string, object> dicParams = CodeCommon.GetUrlParams(url);
                                dicParams.Add("UserId", UserInfo.UserID);
                                DTCash.Logger.NodeLog.NodeLogHandler.AddLog("ajchuifu", "huifuinfo", "urljson", "开户",
                                    NodeLogType.info, dicParams);
                                resultResPonse.code = 1;
                                resultResPonse.data = new
                                {
                                    hid = guid,
                                    url,
                                };
                                return Send(resultResPonse);
                            }
                            catch (Exception ex)
                            {
                                LogHelper.WriteLogError("汇付天下注册接口异常 异常原因:",
                                    "汇付天下注册接口异常 user信息:" + Uranus.Common.CodeCommon.Serialize(UserInfo), ex);
                            }

                            #endregion
                        }
                        else if (type.ToLower() == "mercash")
                        {
                            #region 商户代取现

                            BaseResponse transferResponse =
                                Service.User.GetUser().InsertUserTransfer(new UserTransferRequest
                                {
                                    userTransferDTO = new UserTransferDTO()
                                    {
                                        UserTransferTypeID = UserTransferType.Cash,
                                        UserID = UserInfo.UserID,
                                        Price = userShopInfo.CashPrice,
                                        Fee = userShopInfo.Fee,
                                        OrderItemID = userShopInfo.OrderItemID,
                                        ExtendNumber = userShopInfo.UserBankCardID,
                                        UserTransferStatusID = UserTransferStatus.New,
                                        FinishTime = new DateTime(1900, 1, 1),
                                        UserTransferPropertyID = 0
                                    }
                                });
                            if (transferResponse.Code != 1)
                            {
                                resultResPonse.code = 007;
                                return Send(resultResPonse);
                            }
                            try
                            {
                                string ordId = transferResponse.ResposeData;
                                string guid = Guid.NewGuid().ToString("N");
                                TransactionResultCommon transactionResultCommon = new TransactionResultCommon()
                                {
                                    CmdID = CmdID.MerCash,
                                    OrdID = ordId,
                                    UserID = UserInfo.UserID
                                };
                                CacheRedis.AddCache(new TimeSpan(5, 0, 0),
                                    CacheItem.InitByIdentity("HuiFuTransaction", guid).Other.other,
                                    transactionResultCommon);
                                SourceHandle(SourceOperation.Cash, string.Empty, int.Parse(ordId),
                                    Uranus.Common.Validates.GetInt(GetParmsKey<string>("sourceTypeId")));

                                ChinapnrTransaction chinapnrTransaction = new ChinapnrTransaction();
                                string url = chinapnrTransaction.MerCash(ordId, UserInfo.HuiFuCode,
                                    userShopInfo.CashPrice.ToString("f2"), "0.00", "MDT000001",
                                    ChinapnrCommon.ChinaResponseUrl, ChinapnrCommon.ChinaReceiveUrl, "",
                                    UserInfo.UserID.ToString(), "{\"FeeObjFlag\":\"M\",\"FeeAcctId\":\"MDT000001\"}");
                                //LogHelper.WriteLogInfo("获取汇付商户代取现地址mercash", url, new { UserInfo.UserID, OrdId = ordId });
                                Dictionary<string, object> dicParams = CodeCommon.GetUrlParams(url);
                                dicParams.Add("OrdId", ordId);
                                dicParams.Add("UserId", UserInfo.UserID);
                                DTCash.Logger.NodeLog.NodeLogHandler.AddLog("ajchuifu", "huifuinfo", "urljson", "商户代取现",
                                    NodeLogType.info, dicParams);
                                resultResPonse.code = 1;
                                resultResPonse.data = new { hid = guid, url = url };
                                return Send(resultResPonse);
                            }
                            catch (Exception ex)
                            {
                                LogHelper.WriteLogError("汇付天下商户代取现接口异常 异常原因:",
                                    "汇付天下商户代取现接口异常 user信息:" + Uranus.Common.CodeCommon.Serialize(UserInfo), ex);
                            }

                            #endregion
                        }
                        else if (type.ToLower() == "currentmercash")
                        {
                            #region 商户代取现

                            string sourceTypeId = GetParmsKey<string>("sourceTypeId");
                            MerCashResponse merCashResponse = ControllerCommon.CurrentProductMerCash(UserInfo, userShopInfo.CashPrice,
                                userShopInfo.UserBankCardID,
                                (ordId) =>
                                {
                                    SourceHandle(SourceOperation.Cash, string.Empty, int.Parse(ordId),
                                        Uranus.Common.Validates.GetInt(sourceTypeId));
                                });
                            if (merCashResponse.IsSuccess)
                            {
                                resultResPonse.code = 1;
                                resultResPonse.data = new
                                {
                                    hid = merCashResponse.TranscationHid,
                                    respCode = merCashResponse.RespCode,
                                    respDesc = merCashResponse.RespDesc,
                                    ordId = merCashResponse.OrdId
                                };
                            }
                            else
                            {
                                resultResPonse.code = 0;
                                resultResPonse.data = merCashResponse.ResultMsg;
                            }
                            return Send(resultResPonse);

                            #endregion
                        }
                        else if (type.ToLower() == "cash")
                        {
                            #region 提现

                            BaseResponse transferResponse =
                                Uranus.Service.User.GetUser().InsertUserTransfer(new UserTransferRequest
                                {
                                    userTransferDTO = new UserTransferDTO()
                                    {
                                        UserTransferTypeID = UserTransferType.Cash,
                                        UserID = UserInfo.UserID,
                                        Price = userShopInfo.CashPrice,
                                        Fee = userShopInfo.Fee,
                                        OrderItemID = 0,
                                        ExtendNumber = userShopInfo.UserBankCardID,
                                        UserTransferStatusID = UserTransferStatus.New,
                                        FinishTime = new DateTime(1900, 1, 1),
                                        UserTransferPropertyID = 0
                                    }
                                });

                            if (transferResponse.Code != 1)
                            {
                                resultResPonse.code = 007;
                                return Send(resultResPonse);
                            }
                            try
                            {
                                string guid = Guid.NewGuid().ToString("N");
                                string ordId = transferResponse.ResposeData;
                                TransactionResultCommon transactionResultCommon = new TransactionResultCommon()
                                {
                                    CmdID = CmdID.Cash,
                                    OrdID = ordId,
                                    UserID = UserInfo.UserID,
                                };
                                CacheRedis.AddCache(new TimeSpan(5, 0, 0),
                                    CacheItem.InitByIdentity("HuiFuTransaction", guid).Other.other,
                                    transactionResultCommon);
                                SourceHandle(SourceOperation.Cash, string.Empty, int.Parse(ordId),
                                    Uranus.Common.Validates.GetInt(GetParmsKey<string>("sourceTypeId")));
                                ChinapnrTransaction chinapnrTransaction = new ChinapnrTransaction();
                                chinapnrTransaction.PageType = pagetype;
                                string url = chinapnrTransaction.Cash(ordId, UserInfo.HuiFuCode,
                                    userShopInfo.CashPrice.ToString("f2"), userShopInfo.Fee.ToString("f2"), "",
                                    userShopInfo.CardNo,
                                    ChinapnrCommon.ChinaResponseUrl, ChinapnrCommon.ChinaReceiveUrl, "",
                                    UserInfo.UserID.ToString(), "");

                                LogHelper.WriteLogInfo("获取汇付提现地址cash", url, new { UserInfo.UserID, OrdId = ordId });
                                Dictionary<string, object> dicParams = CodeCommon.GetUrlParams(url);
                                dicParams.Add("OrdId", ordId);
                                dicParams.Add("UserId", UserInfo.UserID);
                                DTCash.Logger.NodeLog.NodeLogHandler.AddLog("ajchuifu", "huifuinfo", "urljson", "提现",
                                    NodeLogType.info, dicParams);
                                resultResPonse.code = 1;
                                resultResPonse.data = new { hid = guid, url = url };
                                return Send(resultResPonse);
                            }
                            catch (Exception ex)
                            {
                                LogHelper.WriteLogError("汇付天下提现接口异常 异常原因:",
                                    "汇付天下提现接口异常 user信息:" + Uranus.Common.CodeCommon.Serialize(UserInfo), ex);
                            }

                            #endregion
                        }
                        else if (type.ToLower() == "quickcash")
                        {
                            #region 快提

                            //DateTime time = new DateTime();
                            //int huifuid = HuiFuAddLog(UserInfo.UserId, userShopInfo.CashPrice, userShopInfo.Fee, 0,
                            //    userShopInfo.UserBankCardID, DTCash.Service.PaymentService.HuiFuOperationType.Withdrawal, out time, 24);
                            BaseResponse transferResponse =
                                Uranus.Service.User.GetUser().InsertUserTransfer(new UserTransferRequest
                                {
                                    userTransferDTO = new UserTransferDTO()
                                    {
                                        UserTransferTypeID = UserTransferType.Cash,
                                        UserID = UserInfo.UserID,
                                        Price = userShopInfo.CashPrice,
                                        Fee = userShopInfo.Fee,
                                        OrderItemID = 0,
                                        ExtendNumber = userShopInfo.UserBankCardID,
                                        UserTransferStatusID = UserTransferStatus.New,
                                        FinishTime = new DateTime(1900, 1, 1),
                                        UserTransferPropertyID = 2
                                    }
                                });
                            if (transferResponse.Code != 1)
                            {
                                resultResPonse.code = 007;
                                return Send(resultResPonse);
                            }
                            try
                            {
                                string ordId = transferResponse.ResposeData;
                                string guid = Guid.NewGuid().ToString("N");
                                TransactionResultCommon transactionResultCommon = new TransactionResultCommon()
                                {
                                    CmdID = CmdID.QuickCash,
                                    OrdID = ordId,
                                    UserID = UserInfo.UserID,
                                };
                                CacheRedis.AddCache(new TimeSpan(5, 0, 0),
                                    CacheItem.InitByIdentity("HuiFuTransaction", guid).Other.other,
                                    transactionResultCommon);
                                SourceHandle(SourceOperation.Cash, string.Empty, int.Parse(ordId),
                                    Uranus.Common.Validates.GetInt(GetParmsKey<string>("sourceTypeId")));

                                ChinapnrTransaction chinapnrTransaction = new ChinapnrTransaction();
                                chinapnrTransaction.PageType = pagetype;
                                string url = chinapnrTransaction.Cash(transferResponse.ResposeData, UserInfo.HuiFuCode,
                                    userShopInfo.CashPrice.ToString("f2"), userShopInfo.Fee.ToString("f2"), "MDT000001",
                                    userShopInfo.CardNo,
                                    ChinapnrCommon.ChinaResponseUrl, ChinapnrCommon.ChinaReceiveUrl, "",
                                    UserInfo.UserID.ToString(),
                                    "[{\"FeeObjFlag\":\"M\",\"FeeAcctId\":\"MDT000001\",\"CashChl\":\"IMMEDIATE\"}]");

                                //LogHelper.WriteLogInfo("获取汇付快提地址quickcash", url, new { UserInfo.UserID, OrdId = ordId });
                                Dictionary<string, object> dicParams = CodeCommon.GetUrlParams(url);
                                dicParams.Add("OrdId", ordId);
                                dicParams.Add("UserId", UserInfo.UserID);
                                DTCash.Logger.NodeLog.NodeLogHandler.AddLog("ajchuifu", "huifuinfo", "urljson", "快速提现",
                                    NodeLogType.info, dicParams);
                                resultResPonse.code = 1;
                                resultResPonse.data = new { hid = guid, url };
                                return Send(resultResPonse);
                            }
                            catch (Exception ex)
                            {
                                LogHelper.WriteLogError("汇付天下快提接口异常 异常原因:",
                                    "汇付天下快提接口异常 user信息:" + Uranus.Common.CodeCommon.Serialize(UserInfo), ex);
                            }

                            #endregion
                        }
                        else if (type.ToLower() == "netsave")
                        {
                            #region 充值

                            if (!CommonModel.VerificationVersion())
                            {
                                resultResPonse.code = 307;
                                return Send(resultResPonse);
                            }
                            BaseResponse transferResponse =
                                Uranus.Service.User.GetUser().InsertUserTransfer(new UserTransferRequest
                                {
                                    userTransferDTO = new UserTransferDTO()
                                    {
                                        UserTransferTypeID = UserTransferType.Recharge,
                                        UserID = UserInfo.UserID,
                                        Price = userShopInfo.ChargePrice,
                                        Fee = userShopInfo.Fee,
                                        OrderItemID = 0,
                                        ExtendNumber = userShopInfo.UserBankCardID,
                                        UserTransferStatusID = UserTransferStatus.New,
                                        FinishTime = new DateTime(1900, 1, 1),
                                        UserTransferPropertyID = 3
                                    }
                                });
                            if (transferResponse.Code != 1)
                            {
                                resultResPonse.code = 007;
                                return Send(resultResPonse);
                            }
                            try
                            {
                                string ordId = transferResponse.ResposeData;
                                string guid = Guid.NewGuid().ToString("N");
                                TransactionResultCommon transactionResultCommon = new TransactionResultCommon()
                                {
                                    CmdID = CmdID.NetSave,
                                    OrdID = ordId,
                                    UserID = UserInfo.UserID,
                                };
                                CacheRedis.AddCache(new TimeSpan(5, 0, 0),
                                    CacheItem.InitByIdentity("HuiFuTransaction", guid).Other.other,
                                    transactionResultCommon);
                                SourceHandle(SourceOperation.Recharge, string.Empty, int.Parse(ordId),
                                    Uranus.Common.Validates.GetInt(GetParmsKey<string>("sourceTypeId")));

                                ChinapnrTransaction chinapnrTransaction = new ChinapnrTransaction();
                                chinapnrTransaction.PageType = pagetype;
                                DateTime time = DateTime.Now;
                                string url =
                                    new ChinapnrTransaction() { PageType = pagetype }.QPNetSave(UserInfo.HuiFuCode,
                                        ordId,
                                        time.ToString("yyyyMMdd"),
                                        userShopInfo.BankNumber, "",
                                        UserInfo.IDCard.ToLower().Contains("x") ? "" : UserInfo.IDCard,
                                        userShopInfo.ChargePrice.ToString("f2"),
                                        ChinapnrCommon.ChinaResponseUrl,
                                        ChinapnrCommon.ChinaReceiveUrl, UserInfo.UserID + "");

                                //LogHelper.WriteLogInfo("获取汇付快捷充值地址netsave", url, new { UserInfo.UserID, OrdId = ordId });
                                Dictionary<string, object> dicParams = CodeCommon.GetUrlParams(url);
                                dicParams.Add("OrdId", ordId);
                                dicParams.Add("UserId", UserInfo.UserID);
                                DTCash.Logger.NodeLog.NodeLogHandler.AddLog("ajchuifu", "huifuinfo", "urljson", "快捷充值",
                                    NodeLogType.info, dicParams);
                                resultResPonse.code = 1;
                                resultResPonse.data = new { hid = guid, url };
                                return Send(resultResPonse);
                            }
                            catch (Exception ex)
                            {
                                LogHelper.WriteLogError("汇付天下快捷充值接口异常 异常原因:",
                                    "汇付天下快捷充值接口异常 user信息:" + Uranus.Common.CodeCommon.Serialize(UserInfo), ex);
                            }

                            #endregion
                        }
                        else if (type.ToLower() == "initiativetender")
                        {
                            #region initiativetender 投标

                            if (!CommonModel.VerificationVersion())
                            {
                                resultResPonse.code = 122;
                                resultResPonse.data = "后台服务验证失败 不予投标 请联系客服!";
                                return Send(resultResPonse);
                            }

                            #region 生成订单

                            string guid = Guid.NewGuid().ToString("N");
                            /*读取配置中心，若取值和当前用户ID一致,则拥有 开通永久存入权限*/
                            object noviceWhiteUser = ConfigCenterWork.GetConfigValueByOperationType(15); //新手标白名单用户
                            if (noviceWhiteUser != null)
                            {
                                string[] supportarray = noviceWhiteUser.ToString().Split(',');
                                if (!supportarray.Contains(UserInfo.UserID.ToString()))
                                {
                                    if (userShopInfo.ProductGroupID == 2 && UserInfo.InvestCount > 0)
                                    {
                                        resultResPonse.code = 0;
                                        resultResPonse.data = "仅限新手用户购买";
                                        return Send(resultResPonse);
                                    }
                                }
                            }
                            else
                            {
                                if (userShopInfo.ProductGroupID == 2 && UserInfo.InvestCount > 0)
                                {
                                    resultResPonse.code = 0;
                                    resultResPonse.data = "仅限新手用户购买";
                                    return Send(resultResPonse);
                                }
                            }
                            InitiativeTenderResponse res = Uranus.Service.Order.GetOrder()
                                .CreateOrderItem(new InitiativeTenderRequest()
                                {
                                    InvestUserID = UserInfo.UserID,
                                    ProductCode = userShopInfo.ProductCode,
                                    OrderItemPrice = userShopInfo.BuyPrice
                                }) ?? new InitiativeTenderResponse();
                            if (res.Code == 1)
                            {
                                //LogHelper.WriteLog(LogEnum.Info, "获取投标地址", string.Format("生成订单号:{0}", res.ResposeData), UserInfo.UserID.ToString());
                            }
                            else
                            {
                                LogHelper.WriteLog(LogEnum.Error, "生成订单失败", string.Format("失败内容:{0}", res.ResposeData),
                                    UserInfo.UserID.ToString());
                                resultResPonse.code = 0;
                                resultResPonse.data = "生成订单失败";
                                return Send(resultResPonse);
                            }
                            int OrderItemID = int.Parse(res.ResposeData);

                            #endregion

                            try
                            {
                                if (userShopInfo.GiftCardID > 0)
                                {
                                    UserGiftCardResponse userGiftCardResponse =
                                        Service.User.GetUser().InvestUseGiftCard(userShopInfo.GiftCardID, OrderItemID);
                                    if (userGiftCardResponse.Code != 1)
                                    {
                                        resultResPonse.code = 0;
                                        resultResPonse.data = "礼品卡使用失败";
                                        return Send(resultResPonse);
                                    }
                                }

                                DateTime time = DateTime.Now;
                                BaseResponse transferResponse =
                                    Uranus.Service.User.GetUser().InsertUserTransfer(new UserTransferRequest
                                    {
                                        userTransferDTO = new UserTransferDTO()
                                        {
                                            UserTransferTypeID = UserTransferType.Bid,
                                            UserID = UserInfo.UserID,
                                            Price = userShopInfo.BuyPrice,
                                            Fee = userShopInfo.Fee,
                                            OrderItemID = OrderItemID,
                                            ExtendNumber = 0,
                                            UserTransferStatusID = UserTransferStatus.New,
                                            FinishTime = new DateTime(1900, 1, 1),
                                            UserTransferPropertyID = 0
                                        }
                                    });
                                if (transferResponse.Code == 1 && !string.IsNullOrEmpty(transferResponse.ResposeData))
                                {
                                    string ordId = transferResponse.ResposeData;
                                    LogHelper.WriteLogInfo("生成汇付天下投标号", ordId + "", new { userid = UserInfo.UserID });
                                    string url = string.Empty;

                                    CacheRedis.AddCache(new TimeSpan(5, 0, 0),
                                        CacheItem.InitByIdentity("HuiFuTransaction", guid).Other.other,
                                        new TransactionResultCommon
                                        {
                                            CmdID = CmdID.InitiativeTender,
                                            OrdID = ordId,
                                            UserID = UserInfo.UserID,
                                            Extension = "普通投标",
                                            OrderItemId = OrderItemID + "",
                                            ProductCode = userShopInfo.ProductCode
                                        });
                                    //CacheRedis.AddCache(new TimeSpan(0, 30, 0), CacheItem.InitByIdentity("HuiFuTransaction",, "true");
                                    ChinapnrTransaction chinapnrTransaction = new ChinapnrTransaction();
                                    chinapnrTransaction.PageType = pagetype;
                                    url = chinapnrTransaction.InitiativeTender(ordId, time.ToString("yyyyMMdd"),
                                        userShopInfo.BuyPrice.ToString("f2"),
                                        UserInfo.HuiFuCode, "0.10",
                                        Uranus.Common.CodeCommon.Serialize(new List<BorrowerCustDTO>
                                        {
                                            new BorrowerCustDTO()
                                            {
                                                BorrowerCustId = userShopInfo.LHuifuCode + "",
                                                BorrowerAmt = userShopInfo.BuyPrice.ToString("f2"),
                                                BorrowerRate = "0.50",
                                                ProId = userShopInfo.ProId
                                            }
                                        }), ordId, ChinapnrCommon.ChinaResponseUrl, ChinapnrCommon.ChinaReceiveUrl,
                                        UserInfo.UserID + "|" + userShopInfo.ProductCode + "|" + OrderItemID, "");

                                    SourceHandle(SourceOperation.InitiativeTender, string.Empty, int.Parse(ordId),
                                        Uranus.Common.Validates.GetInt(GetParmsKey<string>("sourceTypeId")));
                                    //LogHelper.WriteLogInfo("获取汇付投标地址", url, new { UserInfo.UserID, OrdId = ordId });
                                    Dictionary<string, object> dicParams = CodeCommon.GetUrlParams(url);
                                    dicParams.Add("OrdId", ordId);
                                    dicParams.Add("UserId", UserInfo.UserID);
                                    DTCash.Logger.NodeLog.NodeLogHandler.AddLog("ajchuifu", "huifuinfo", "urljson", "投标",
                                        NodeLogType.info, dicParams);
                                    resultResPonse.code = 1;
                                    resultResPonse.data = new
                                    {
                                        OrderItemId = OrderItemID,
                                        hid = guid,
                                        url = url
                                    };
                                }
                            }
                            catch (Exception ex)
                            {
                                LogHelper.WriteLogError("汇付天下投标接口异常 异常原因:",
                                    "汇付天下投标接口异常 user信息:" + Uranus.Common.CodeCommon.Serialize(UserInfo), ex);
                                resultResPonse.code = 0;
                                resultResPonse.data = "生成地址失败";
                            }
                            return Send(resultResPonse);

                            #endregion
                        }
                    }
                }
            }
            else
            {
                resultResPonse.code = 118;
            }
            return Send(resultResPonse);
        }

        /// <summary>
        /// 汇付操作确认
        /// </summary>
        /// <param name="gId">确认标识</param>
        /// <returns></returns>
        [HttpPost]
        [UserState(VisitUserStateEnum = VisitUserStateEnum.MustLogin)]
        public ResultResPonse HuiFuConfirm()
        {
            string hid = GetParmsKey<string>("hid");
            if (string.IsNullOrEmpty(hid))
            {
                resultResPonse.code = 3;//参数非法
                return Send(resultResPonse);
            }
            if (!CacheRedis.ExistsRedis(CacheItem.InitByIdentity("HuiFuTransaction", hid).Other.other))
            {
                resultResPonse.code = 0;
                resultResPonse.data = "交易不存在";
                return Send(resultResPonse);
            }
            TransactionResultCommon transaction = CacheRedis.GetCache(CacheItem.InitByIdentity("HuiFuTransaction", hid).Other.other) as TransactionResultCommon ?? new TransactionResultCommon();
            if (transaction.UserID != UserInfo.UserID)
            {
                resultResPonse.code = 2;
                return Send(resultResPonse);
            }
            if (transaction.CmdID == CmdID.UserRegister)
            {
                BaseResponse baseResponse = Service.User.GetUser().VerifyUser(UserInfo.UserID);
                if (baseResponse.Code == 1 && !string.IsNullOrEmpty(baseResponse.ResposeData))
                {
                    try
                    {
                        SourceHandle(Service.OtherService.SourceOperation.RegisterHuiFu, string.Empty, UserInfo.UserID, Uranus.Common.Validates.GetInt(GetParmsKey<string>("sourceTypeId")));
                    }
                    catch (Exception ex)
                    {
                        LogHelper.WriteLog(LogEnum.Error, "Source记录异常", UserInfo.UserID.ToString());
                    }
                    CacheRedis.DelCacheKeys(CacheItem.InitByIdentity(transaction.UserID.ToString(), "GetUserByUserID").User.Info.HideExt());
                    resultResPonse.code = 1;
                    resultResPonse.data = new
                    {
                        type = "userregister"
                    };
                    return Send(resultResPonse);
                }
                else if (baseResponse.Code == 3)//未取到huifucode
                {
                    resultResPonse.code = 245;
                    return Send(resultResPonse);
                }
                resultResPonse.code = 304;
                return Send(resultResPonse);
            }
            else if (transaction.CmdID == CmdID.NetSave || transaction.CmdID == CmdID.Cash || transaction.CmdID == CmdID.MerCash ||
                transaction.CmdID == CmdID.QuickCash)
            {
                UserTransferResponse responseTransfer =
                    Uranus.Service.User.GetUser()
                            .GetUserTransferDTOByTransferID(int.Parse(transaction.OrdID));
                if (responseTransfer.Code != 1)
                {
                    resultResPonse.code = 7;
                }
                else if (responseTransfer.UserTransfer.UserTransferStatusID == UserTransferStatus.New)
                {
                    resultResPonse.code = 245;
                    return Send(resultResPonse);
                }
                else if (responseTransfer.UserTransfer.UserTransferStatusID == UserTransferStatus.Succeed)
                {
                    resultResPonse.code = 1;
                    return Send(resultResPonse);
                }
                else
                {
                    resultResPonse.code = 304;
                    return Send(resultResPonse);
                }
                return Send(resultResPonse);
            }
            else if (transaction.CmdID == CmdID.InitiativeTender)
            {
                UserTransferResponse responseTransfer =
                    Uranus.Service.User.GetUser()
                          .GetUserTransferDTOByTransferID(int.Parse(transaction.OrdID));
                if (responseTransfer.Code != 1)
                {
                    resultResPonse.code = 7;
                }
                else if (responseTransfer.UserTransfer.UserTransferStatusID == UserTransferStatus.New)
                {
                    resultResPonse.code = 245;
                }
                else if (responseTransfer.UserTransfer.UserTransferStatusID == UserTransferStatus.Succeed)
                {
                    CacheRedis.DelCacheKeys(
                        CacheItem.InitByIdentity(transaction.OrderItemId, "GetOrderItemByOrderId")
                                    .OrderItem.Info.HideExt());
                    OrderItemDTO orderItemDTO = Uranus.Service.Order.GetOrder().GetOrderItemById(int.Parse(transaction.OrderItemId));
                    if (orderItemDTO == null || orderItemDTO.OrderItemStatusID < 2)
                    {
                        resultResPonse.code = 303;
                        return Send(resultResPonse);
                    }
                    if (orderItemDTO.OrderItemStatusID == 3)
                    {
                        object re = CacheRedis.GetCache(
                            CacheItem.InitByIdentity(transaction.OrdID, "transferfail").Other.other.HideExt());
                        if (re != null && !string.IsNullOrEmpty(re.ToString()))
                        {
                            resultResPonse.code = 303;
                            resultResPonse.data = re.ToString();
                            return Send(resultResPonse);
                        }
                        else
                        {
                            resultResPonse.code = 303;
                            resultResPonse.data = "交易失败";
                            return Send(resultResPonse);
                        }
                    }

                    resultResPonse.code = 1;
                    resultResPonse.data = new
                    {
                        type = "initiativetender",
                        orderitemid = transaction.OrderItemId,
                        productcode = transaction.ProductCode,
                    };
                }
                else
                {
                    resultResPonse.code = 304;
                }
                return Send(resultResPonse);
            }
            resultResPonse.code = 2;
            return Send(resultResPonse);
        }
    }
}